package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.CashHistoryListAdapter;
import com.iexbuy.ihg.adapter.DealHistoryDetailListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.CashHistoryListObject;
import com.iexbuy.ihg.entity.CashHistoryListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailObject;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeCashRecordsFragment extends BaseFragment {

	private static WeDeCashRecordsFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.cashBalance)
	private TextView cashBalance;

	private LinearLayout listHeaderView;

	private List<CashHistoryListObject> CashHistoryListObjects = new ArrayList<CashHistoryListObject>();
	private int pageNo = 0;
	private CashHistoryListAdapter cashHistoryListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_cash_records, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_cashs));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		listHeaderView = (LinearLayout) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_wede_cash_records_header, null);
		ViewUtils.inject(this, listHeaderView);
		listView.addHeaderView(listHeaderView);

		cashBalance.setText(App.sPreferences.getCashBalance() + "元");
		msgListLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initListData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				pullToRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
			}
		});

		msgListLayout.autoRefresh();


	}


	public void initListData() {

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		Log.d("aa",App.sPreferences.getUserId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_CASH_HISTORY, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						CashHistoryListObjectResponce rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), CashHistoryListObjectResponce.class);
						if (pageNo == 0) {
							CashHistoryListObjects.clear();
						}
						CashHistoryListObjects.addAll(rep.getData());
						Log.d("aa",CashHistoryListObjects+"");
						initListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListLayout.refreshFinish(PullToRefreshLayout.FAIL);
					}
				});
	}

	private void initListView() {

		//我的余额明细
		if (cashHistoryListAdapter == null) {
			cashHistoryListAdapter = new CashHistoryListAdapter(mBaseActivity, CashHistoryListObjects);
			listView.setAdapter(cashHistoryListAdapter);
		}
		cashHistoryListAdapter.notifyDataSetChanged();
	}

	/**
	 * 我要提现
	 * @param view
	 */
	@OnClick(R.id.withdrewCashBtn)
	public void withdrewCashBtnClick(View view) {
		if (new BigDecimal(App.sPreferences.getCashBalance()).compareTo(BigDecimal.ZERO) <= 0) {
			ToastUtils.toast(mBaseActivity, "剩余资金为0");
			return;
		}
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashWithdrawFragment.class);
	}

	/**
	 * 兑现排号
	 * @param view
	 */
	@OnClick(R.id.cash_numeralLayout)
	public void cash_numeralLayoutBtnClick(View view) {

		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeMianDanQueueRecordsFragment.class);
	}

	/**
	 * 兑现记录
	 * @param view
	 */
	@OnClick(R.id.cash_recordLayout)
	public void cash_recordLayoutBtnClick(View view) {
		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeMianDanRecordsFragment.class);

	}

	/**
	 * 提现记录
	 * @param view
	 */
	@OnClick(R.id.withdraw_recordLayout)
	public void withdraw_recordLayoutBtnClick(View view) {

		CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashWithdrawRecordsFragment.class);
	}

	/**
	 * 账户管理
	 * @param view
	 */
	@OnClick(R.id.account_managementLayout)
	public void account_managementLayoutBtnClick(View view) {

		//CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeCashWithdrawFragment.class);
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeCashRecordsFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
